//
// Copyright (c) 2014, 保留所有权利。
//
// AgentContext.java
//
// 作    者：王红燕
// 当前版本：1.0
// 2014 下午5:31:25
//
// 
//
package com.vanke.agent.sync;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.log4j.Logger;

import com.common.api.ApiServer;
import com.vanke.agent.C;
import com.vanke.agent.bean.Module;
import com.vanke.agent.common.ProjectUtil;
import com.vanke.agent.tools.MonitorClient;

/**
 *
 */
public class AgentContext {
	private static Logger log = Logger.getLogger(AgentContext.class);

	public static class SyncConfig {
		public Date beginTime;
		public Date modifyTime;
		public String cfgKey;
	}

	protected static SyncConfig bookingCfg = new SyncConfig();
	protected static SyncConfig orderCfg = new SyncConfig();
	protected static SyncConfig contractCfg = new SyncConfig();
	protected static SyncConfig saleServiceCfg = new SyncConfig();
	protected static SyncConfig getinCfg = new SyncConfig();
	protected static SyncConfig opportunityCfg = new SyncConfig();

	protected static SyncConfig customerCfg = new SyncConfig();
	protected static SyncConfig memberCfg = new SyncConfig();
	protected static SyncConfig pointCfg = new SyncConfig();
	protected static SyncConfig receiveCfg = new SyncConfig();
	protected static long intervalIntime;

	protected static int rebackOffset = -180; // 回溯时间，单位分钟；负数表示往前

	protected static final int BACK_HOURS = -180;
	protected static final int BACK_DAYS = -4320;

	protected static DateFormat syncTimeFormat = new SimpleDateFormat("HH:mm");

	protected static ApiServer apiServer = new ApiServer();

	protected static Module module = null;

	protected void printInfo() {
		//		log.info("程序启动......");
		//		log.info("数据中心增量同步 V3.0（2014-12-26）");
	} //~

	public static void writeMonitorLog(String level, String subName, String msg) {
		writeMonitorLog(MonitorClient.LOGTYPE_RUNNING, level, subName, msg);
	}

	public static void writeMonitorLog(int type, String level, String subName,
			String msg) {
		if (module == null) {
			log.warn("uninit monitor");
			return;
		}
		MonitorClient.log(module.getKey(), type, level, subName, msg);
	}

	public static void exit(String msg) {
		log.fatal(msg);
		writeMonitorLog(C.LOG_LEVEL_FATAL, "DC.SYNC", msg);
		System.exit(1);
	}

	/*
	 * 按项目GUID取项目Code
	 */
	protected String getProjectCode(Connection myCon, String projGUID)
			throws SQLException {
		return ProjectUtil.getProjectCode(myCon, projGUID);
	} //~getProjectCode
}
